package com.vmove.childtrain.common;

import android.os.Bundle;

import com.vmove.childtrain.utils.ByteUtil;
import com.vmove.childtrain.utils.LogUtils;


/**
 * 需要串口通信的界面，继承该类，提供串口通信接口
 */
public abstract class BaseSerialActivity extends BaseActivity {

    protected CustomSerialDriver customSerialDriver;
    protected boolean isSerialConnect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customSerialDriver = CustomSerialDriver.getInstance(AppContext.driver);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!customSerialDriver.isConnect()) {
            customSerialDriver.initDriver();
        }
        registLister();
    }

    public CustomSerialDriver getCustomSerialDriver(){
        return customSerialDriver;
    }


    byte[] dataTemp;

    private void registLister() {
        if (null==customSerialDriver) return;
        isSerialConnect = customSerialDriver.isConnect();
        customSerialDriver.setDataListener(new CustomSerialDriver.Listener() {
            @Override
            public void onNewData(byte[] data) {
                byte[] dataToSend = data;
                if (dataTemp!=null && dataTemp.length>0){
                    dataToSend = ByteUtil.connectBytes(dataTemp, data);
                    LogUtils.e("拼接的包："+ByteUtil.bytesToHexString(dataToSend, true));
                }
                //包头A5 + 长度+*** 包尾5A
                int currentHeadIndex=0;
                int dataLength = dataToSend.length;
                int tempCount=0;
                while (currentHeadIndex<dataLength){
                    if (dataToSend[currentHeadIndex]!=(byte) 0xA5){
                        currentHeadIndex++;
                        continue;
                    }
                    //包头在最后一个字节时
                    if (currentHeadIndex+1==dataLength){
                        tempCount=1;
                        break;
                    }
                    //单包长度
                    int innerLen = dataToSend[currentHeadIndex + 1];
                    //最后一个包不全时
                    if (innerLen > dataLength-currentHeadIndex){
                        tempCount=dataLength-currentHeadIndex;
                        break;
                    }
                    //收到一个或者多个包
                    byte[] d = new byte[innerLen];
                    System.arraycopy(dataToSend,currentHeadIndex,d,0,innerLen);
                    onSerialReceivedData(d);
                    currentHeadIndex+=innerLen;
                }
                if (tempCount>0){
                    dataTemp = new byte[tempCount];
                    System.arraycopy(dataToSend, dataLength-tempCount, dataTemp, 0, tempCount);
                }else {
                    dataTemp=null;
                }
            }

            @Override
            public void onRunError(Exception e) {

            }
        });
    }

    public void writeSerialByte(byte[] data){
        customSerialDriver.writeData(data);
    }

    /**
     * 实现该方法，接受串口数据
     * @param data
     */
    public abstract void onSerialReceivedData(byte[] data);
}
